package com.zhou.common.mybatis.template.shiro.controller;

import com.zhou.common.lib.model.Response;
import com.zhou.common.lib.util.StringUtils;
import com.zhou.common.mybatis.template.shiro.service.impl.SysUserOnlineServiceImpl;
import com.zhou.common.mybatis.template.shiro.utils.ShiroUtils;
import com.zhou.common.operator.log.core.annotation.OperateLog;
import com.zhou.common.operator.log.core.constant.OperateActionEnum;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.zhou.common.lib.model.Response.error;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Controller
public class SysLoginController {

    @Autowired
    private SysUserOnlineServiceImpl userOnlineService;

    @OperateLog(action = OperateActionEnum.LOGIN,
            operateInstruction = "#{#username}登录#{#exception==null?(#resp.code==0?'成功':'失败'+#resp.msg):'失败'+#exception.message}",
            operateSystem = "shiro-test",
            operateType = "operateType",
            operateModule = "shiro"
    )
    @PostMapping("/login")
    @ResponseBody
    public Response ajaxLogin(String username, String password, Boolean rememberMe) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            return Response.success();
        } catch (AuthenticationException e) {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage())) {
                msg = e.getMessage();
            }
            return error(msg);
        }
    }


    @OperateLog(action = OperateActionEnum.LOGOUT,
            operateInstruction = "#{#username}登出#{#exception==null?(#resp.code==0?'成功':'失败'+#resp.msg):'失败'+#exception.message}",
            operateSystem = "shiro-test",
            operateType = "operateType",
            operateModule = "shiro"
    )
    @PostMapping("/logout")
    @ResponseBody
    public Response ajaxLogout() {
        UsernamePasswordToken token = new UsernamePasswordToken();
        Subject subject = SecurityUtils.getSubject();
        Session session = ShiroUtils.getSession();
        String loginName = ShiroUtils.getLoginName();
        try {
            subject.logout();
            userOnlineService.removeUserCache(loginName, session.getId().toString());
            return Response.success();
        } catch (AuthenticationException e) {
            String msg = "登出失败";
            if (StringUtils.isNotEmpty(e.getMessage())) {
                msg = e.getMessage();
            }
            return error(msg);
        }
    }


}
